<template>
  <div>

    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>需求管理</el-breadcrumb-item>
      <el-breadcrumb-item>需求格式列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入需求格式名搜索"
                    v-model="searchFormat"
                    clearable
                    @clear="getFormatList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getFormatList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addFormat">添加需求格式</el-button>
        </el-col>
      </el-row>
      <el-table :data="formatList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="需求格式名称" prop="name"></el-table-column>
        <el-table-column label="创建时间" prop="createTime">
          <template slot-scope="scope">
            {{ $moment(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column label="格式类型" prop="type">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 0">视频</span>
            <span v-if="scope.row.type === 1">图片</span>
            <span v-if="scope.row.type === 2">音频</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" style="display: inline-block;" width="170px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改需求格式信息" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                         @click="showEditDialog(scope.row.demandFormatId)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除需求" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" @click="deleteFormat(scope.row.demandFormatId)"
                         size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>

    <el-dialog
      title="修改需求信息"
      :visible.sync="editDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="editDialogClosed">
      <el-form :model="editForm" ref="editFormRef" label-width="100px">
        <el-form-item label="需求格式名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属类型" prop="type">
          <el-select v-model="type" clearable placeholder="请选择" @change="typeSelect">
            <el-option
              v-for="item in typeKeys"
              :key="item.type"
              :label="item.typeName"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="changeFormat(editForm)">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="添加需求格式"
      :visible.sync="addDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="addDialogClosed">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="110px">
        <el-form-item label="需求格式名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属类型" prop="type">
          <el-select v-model="type" clearable placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.typeId"
              :label="item.typeName"
              :value="item.typeId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitAddCategory()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
    import {
        addFormat,
        changeFormatById,
        deleteFormatById,
        searchFormatById,
        searchFormatByPage
    } from "../../assets/api/demandFormatApi";
    import {getAllTypes} from "../../assets/api/typesApi";

    export default {
        name: "demandFormat",
        data() {
            return {
                formatList: [],
                searchFormat: '',
                pageNum: 1,
                pageSize: 5,
                total: 0,
                editDialogVisible: false,
                addDialogVisible: false,
                addFormRules: {name: {required: true, message: '请输入需求格式名称', trigger: 'blur'},},
                editForm: {},
                addForm: {name: '', type: 0},
                typeList: [],
                typeName: '',
                type: 0,
                typeKeys: [{type: 0, typeName: '视频'},
                    {type: 1, typeName: '图片'},
                    {type: 2, typeName: '音频'}],
            }
        },
        methods: {
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getFormatList();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getFormatList();
            },
            getFormatList() {
                let data = {};
                data.name = this.searchFormat;
                searchFormatByPage(this.pageNum, this.pageSize, data).then(res => {
                    if (res.code === 200) {
                        this.formatList = res.data.records;
                        this.total = res.data.total;
                    }
                })
            },
            showEditDialog(demandFormatId) {
                this.editDialogVisible = true;
                let data = {};
                data.demandFormatId = demandFormatId;
                searchFormatById(data).then((res) => {
                    this.editForm = res.data;
                })
            },
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
            },
            typeSelect() {
                this.editForm.type = this.type;
            },
            changeFormat() {
                changeFormatById(this.editForm).then((res) => {
                    if (res.code !== 200) {
                        this.$message.error(res.message);
                    } else {
                        this.editDialogVisible = false;
                        this.getFormatList();
                        this.$message.success(res.message);
                    }

                })
            },
            addFormat() {
                this.getTypesList();
                this.addDialogVisible = true;
            },
            addDialogClosed() {
                this.$refs.addFormRef.resetFields();
                this.name = '';
                this.typeName = '';
                this.type = 0;
            },
            submitAddCategory() {
                this.$refs.addFormRef.validate(valid => {
                    if (!valid) return;
                    this.addForm.type = this.type;
                    addFormat(this.addForm).then((res) => {
                        if (res.code !== 200) {
                            return this.$message.error(res.message);
                        }
                        this.$message.success(res.message);
                        this.addDialogVisible = false;
                        this.getFormatList();
                    })
                })
            },
            getTypesList() {
                getAllTypes().then((res) => {
                    if (res.code !== 200) {
                        return this.$message.error("查询类型失败！");
                    }
                    this.typeList = res.data;
                })
            },
            deleteFormat(id) {
                const confirmResult = this.$confirm(
                    '此操作将永久删除该格式, 是否继续?',
                    '提示',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                    let data = {};
                    data.demandFormatId = id;
                    deleteFormatById(data).then(res => {
                        if (res.code === 200) {
                            this.$message.success(res.message);
                            this.getFormatList();
                        } else {
                            this.$message.error(res.message);
                        }
                    })
                }).catch(() => {
                    this.$message.info('已取消删除');
                });
            },
        },
        created() {
            this.getFormatList();
        }
    }
</script>

<style scoped>
  @import "../../styles/css/global.css";
</style>
